package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.pojo.AdChannel;
import com.heima.admin.service.AdChannelService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.dtos.ChannelQueryDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-03-22
 */
@Slf4j
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 分页查询频道列表信息
     *
     * @param dto 查询条件
     * @return 频道列表信息
     */
    @Override
    public PageResult<AdChannel> getListByCondition(ChannelQueryDTO dto) {
        if (dto == null) {
            return new PageResult<>(null, null, null, null);
        }
        Page<AdChannel> iPage = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(dto.getName()), AdChannel::getName, dto.getName());

        this.page(iPage, queryWrapper);

        if (CollectionUtils.isEmpty(iPage.getRecords())) {
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        return new PageResult<AdChannel>(dto.getPage(), dto.getSize(), iPage.getTotal(), iPage.getRecords());
    }

    /**
     * 保存频道信息
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveByDTO(ChannelDTO dto) {
        log.info("dto = {}", dto);
        if (dto == null
                || StringUtils.isEmpty(dto.getName())
                || dto.getStatus() == null) {
            log.error("缺少必填参数: {},", dto);
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        AdChannel channel = BeanHelper.copyProperties(dto, AdChannel.class);
        // 保存频道列表信息, 并根据保存结果进行返回
        return this.save(channel) ? ResponseResult.ok() : ResponseResult.error(AppHttpCodeEnum.UPDATE_ERROR);
    }

    /**
     * 根据频道id修改频道信息
     *
     * @param dto 前端参数
     * @return 响应
     */
    @Override
    public ResponseResult updateByDTO(ChannelDTO dto) {
        log.info("dto = {}", dto);
        if (dto == null
                || dto.getId() == null
                || StringUtils.isEmpty(dto.getName())
                || dto.getStatus() == null) {
            log.error("缺少必填参数: {},", dto);
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        AdChannel channel = BeanHelper.copyProperties(dto, AdChannel.class);

        // 根据channelId进行新增 如果新增成功返回200 新增失败返回504 数据更新异常
        return this.updateById(channel) ? ResponseResult.ok() : ResponseResult.error(AppHttpCodeEnum.UPDATE_ERROR);
    }
}
